// 全局声明
var globalRequest = "https://www.shenxuanche.com";
var jsonRequest = "https://www.shenxuanche.com/data";
var requestUrl = "https://api.shenxuanche.com";
var comRequest = "https://comment.shenxuanche.com";
var myStorage = window.localStorage;
var bdMapAK = "fsznlk2UphGwxgIG2y9pVcHalLSkePyd";
var userIp = "";
var locationCity;
var userInfo = JSON.parse(localStorage.getItem("dnt"))
var isLogin = !!localStorage.getItem("dnt");
// AJAX封装
// type = get/post url = 请求地址 data = 请求数据 token = 是否需要用户信息 callback = 正确时回调函数  noCallback = NO时回调函数  errrCallback = 错误时回调函数
// get必须为字符串方式传参数  post必须为对象方式传参数
function requestAjax(type, url, data, token, callback, noCallback, errrCallback) {
    if (token === true) {
        if (!userInfo) {
            dialog("用户未登录", aa, '');
            return false;
        } else {
            if (typeof(data) === "string") {
                data = "uid=" + userInfo.userid + "&m=" + userInfo.SessionID + "&username=" + userInfo.username + "&" + data
            } else {
                data.uid = userInfo.userid;
                data.username = userInfo.username;
                data.m = userInfo.SessionID;
            }
        }
    } else {
        if (typeof(data) === "string") {
            data = "uid=" + ((userInfo && userInfo.userid) || "") + "&m=" + ((userInfo && userInfo.SessionID) || "") + "&username=" + ((userInfo && userInfo.username) || "") + "&" + data
        } else {
            data.uid = userInfo.userid || "";
            data.username = userInfo.username || "";
            data.m = userInfo.SessionID || ""
        }
    }

    $.ajax({
        type: type,
        url: url,
        data: data,
        dataType: "json",
        success: function(json) {
            // 请求JSON文件时
            if (url.substr(-5) === '.json') {
                callback(json)
            } else {
                // 请接口时
                if (json.message == "ok") {
                    callback(json)
                } else {

                    if (noCallback) {
                        noCallback(json);
                    } else {

                        if (json.messagestr == "") {
                            prompt("服务器错误，请求失败");
                        } else {
                            prompt(json.messagestr);
                        }
                    }
                }
            }
        },
        error: function() {
            if (errrCallback) {
                errrCallback()
            } else {
                prompt("请求失败，请稍后重试");
            }
        }
    });
}
// 存储本地历史记录
function saveHistory(hisData, tempData) {
    hisData.unshift(tempData)
    hisData.splice(10, 1);
    hisData = JSON.stringify(hisData);
    myStorage.setItem("history", hisData);
}

// 模板渲染
function loadTemp(tempId, target, item) {
    var html = $("#" + tempId).html();
    $.each(item, function(key, val) {
        html = html.replace(new RegExp("{{" + key + "}}", "gim"), val);
    });
    $("." + target).append(html);
}

//页面图片加载失败时 默认显示统一处理
document.addEventListener("error", function(e) {
    var elem = e.target;
    if (elem.tagName.toLowerCase() == "img") {
        //  elem.src = "/h5_page/img/img_error.png";
    }
}, true);

// 截取url参数
function GetUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;
        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");
            if (arr != null && arr[0] == paraName) {
                subjectID = arr[1];
                return arr[1];
            }
        }
        return "";
    } else {
        return "";
    }
}
// 时间戳转换

function formatDateTime(inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};
// 毫秒转化时分秒
function toHHmmss(date) {
    var time;
    var hours = parseInt((date % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((date % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((date % (1000 * 60)) / 1000);
    console.log(seconds);
    time = (hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? ('0' + minutes) : minutes) + ':' + (minutes < 10 ? ('0' + seconds) : seconds);
    // time = (hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? ('0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds);
    return time;
}
// 显示个人中心
function showPersonal() {
    $(".personal-wrap").show();
}

// 用户退出function User_Exit(){
function User_Exit() {
    $.ajax({
        type: "get",
        async: false,
        url: "http://comment.chooseauto.com.cn/chooseauto/exitlogin.aspx",
        dataType: "jsonp",
        jsonp: "callback",
        success: function(json) {
            if (json.message == "ok") {
                CAUserInfo = {};
                // $("#login").show();
                // $("#mine").hide();
                $(".personal img").attr("src", "")
                    // LoginInit();
            }
        }
    });
}